<template>
  <div class="contentbox flex-col">
    <el-card class="card-margin flex-col">
      <el-descriptions
        title="日志记录展示"
        size="medium"
        :column="1"
        :colon="false"
      >
        <el-descriptions-item>
          <div class="taskview-top">
            <span>开始时间：</span>
            <el-date-picker
              v-model="startTime"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
            <span>结束时间：</span>
            <el-date-picker
              v-model="endTime"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
            <span>操作者：</span>
            <el-input v-model="userName" placeholder="请输入检索"> </el-input>
            <span>菜单：</span>
            <el-input v-model="menuName" placeholder="请输入检索"> </el-input>
            <span>功能：</span>
            <el-input v-model="functionName" placeholder="请输入检索">
            </el-input>
            <el-button @click="getOplog" type="primary">搜索</el-button>
          </div>
        </el-descriptions-item>
      </el-descriptions>
      <div class="taskmain flex-col">
        <el-table :data="tableData" tooltip-effect="dark" height="100%">
          <el-table-column
            type="index"
            label="序号"
            width="120"
            :index="table_index"
          >
          </el-table-column>
          <el-table-column prop="CreatedDate" label="操作时间">
          </el-table-column>
          <el-table-column prop="User" label="操作者"> </el-table-column>
          <el-table-column prop="MeumName" label="菜单">
            <template slot-scope="scope">
              {{
                dicMenu.find((item) => item.key === scope.row.MeumName)
                  ?.value || scope.row.MeumName
              }}
            </template>
          </el-table-column>
          <el-table-column prop="ActionName" label="功能">
            <template slot-scope="scope">
              {{
                dicFunc.find((item) => item.key === scope.row.ActionName)
                  ?.value || scope.row.ActionName
              }}
            </template> </el-table-column>
          <el-table-column prop="Path" label="URL"> </el-table-column>
          <el-table-column prop="State" label="状态"> </el-table-column>
          <el-table-column prop="TimeSpan" label="耗时(ms)"> </el-table-column>
          <el-table-column prop="RequestBody" label="请求参数">
          </el-table-column>
          <el-table-column prop="IP" label="IP"> </el-table-column>
        </el-table>
        <el-pagination
          background
          layout="->,total,sizes,prev, pager, next,jumper"
          :total="total"
          :current-page.sync="pageNum"
          :page-sizes="[20, 40, 60, 100]"
          :page-size="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import $http from "../../utils/apiHelper";
export default {
  components: {},
  props: {},
  data() {
    const today = new Date();
    //const tomorrow = new Date(today.getTime() + 24 * 60 * 60 * 1000);
    const sDaysAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);

    //格式化日期
    const format = (date) =>
      `${date.getFullYear()}-${(date.getMonth() + 1)
        .toString()
        .padStart(2, "0")}-${date
        .getDate()
        .toString()
        .padStart(2, "0")} 00:00:00`;

    return {
      dicMenu: [
        { key: "AcquisitionTask", value: "采集任务" },
        { key: "AlarmQueryPage", value: "报警查询" },
        { key: "Completion", value: "采集规则" },
        { key: "Ecdh", value: "登录验证" },
        { key: "EpmSync", value: "EPM同步" },
        { key: "EQDictionary", value: "字典管理" },
        { key: "EqManage", value: "设备管理" },
        { key: "Log", value: "日志文件" },
        { key: "Log", value: "采集规则" },
        { key: "MESExceptionLogs", value: "MES交互异常" },
        { key: "Monitor", value: "监控面板" },
        { key: "OperationalLog", value: "操作日志" },
        { key: "EqManage", value: "设备管理" },
        { key: "PLCMonitor", value: "设备管理" },
        { key: "Script", value: "采集规则" },
        { key: "User", value: "用户管理" },
      ],
      dicFunc: [
        { key: "GetAcquisitionTask", value: "获取采集任务" },
        { key: "SetScripStatus", value: "更改脚本状态" },
        { key: "GetContent", value: "获取采集任务详情" },
        { key: "Complete", value: "完成" },
        { key: "Signature", value: "签名" },
        { key: "Hover", value: "悬浮效果" },
        { key: "CodeCheck", value: "代码检查" },
        { key: "FormatCode", value: "格式化代码" },
        { key: "BuildCode", value: "编译代码" },
        { key: "BuildBaseCode", value: "编译前置代码" },
        { key: "BuildACECode", value: "编译ACE平台前置代码" },
        { key: "DemoCode", value: "获取示例代码" },
        { key: "Dh", value: "密钥交换" },
        { key: "GetPublicKey", value: "获取公钥" },
        { key: "Login", value: "登录" },
        { key: "ssoLogin", value: "SSO登录" },
        { key: "totplogin", value: "totp登录" },
        { key: "GetUserInfo", value: "获取登录信息" },
        { key: "EpmSync", value: "同步EPM结构" },
        { key: "GetProcessInfo", value: "查询工序字典" },
        { key: "InsertProcessInfo", value: "新增工序字典" },
        { key: "DeleteProcessInfo", value: "删除工序字典" },
        { key: "UpdateProcessInfo", value: "修改工序信息" },
        { key: "GetEqCategoryInfo", value: "查询设备分类字典" },
        { key: "InsertEqCategoryInfo", value: "新增设备分类字典" },
        { key: "DeleteEqCategoryInfo", value: "删除设备分类字典" },
        { key: "UpdateEqCategoryInfo", value: "修改设备分类字典" },
        { key: "InsertNetworkInfo", value: "新增网络类型" },
        { key: "GetNetworkInfo", value: "查询双链路配置" },
        { key: "GetSystemConfig", value: "查询双链路配置" },
        { key: "GetLineProcessDevPLCInfo", value: "获取产线工序设备PLC的目录树" },
        { key: "AddLine", value: "新增产线接口" },
        { key: "UpdateLineInfo", value: "更新产线信息" },      
        { key: "DeleteLineEqPlc", value: "删除产线/设备/工序/PLC" },
        { key: "AddLine", value: "新增产线接口" },
        { key: "InsertProcessInfo", value: "产线下新建工序" },
        { key: "DeleteProcessInfo", value: "删除工序" },
        { key: "UpdateProcessInfo", value: "修改工序信息" },
        { key: "AddEq", value: "新增设备" },
        { key: "UpdateEqInfo", value: "更新设备信息接口" },
        { key: "GetEqInfo", value: "获取设备信息" },
        { key: "DownLoadEqData", value: "导出设备菜单" },        
        { key: "ImportEqTree", value: "导入设备菜单" },
        { key: "GetEqInfo", value: "获取设备信息" },
        { key: "AddPlc", value: "新增PLC接口" },
        { key: "GetPlcInfo", value: "获取PLC信息" },
        { key: "UpdatePlcInfo", value: "更新PLC信息接口" },
        { key: "AddVar", value: "新增plc变量接口" },
        { key: "UpdateVar", value: "更新PLC变量信息" },
        { key: "UpdatePlcVarMonitor", value: "更新plc变量监控状态" },
        { key: "UpdatePlcArrayDesc", value: "plc变量数组变更描述" },     
        { key: "GetPlcVar", value: "获取PLC变量" },
        { key: "DeleteVar", value: "删除单个PLC变量" },
        { key: "DeletePlcVarList", value: "删除批量PLC变量" },
        { key: "DownLoadPlcData", value: "plc变量导出" },
        { key: "ImportPlcVariable", value: "导入plc变量数据" },
        { key: "InsertEqGlobalVariable", value: "新增设备/全局变量" },
        { key: "UpdateEqGlobalVarInfo", value: "更新设备/平台变量信息(数组+标量)" },
        { key: "UpdateEqGloVarMonitor", value: "更新设备/平台变量监控状态(数组的单个变量+标量)" },
        { key: "UpdateEqGloArrayDesc", value: "设备/平台变量数组变更描述(数组单个的变量的表述)" },
        { key: "GetEqGlobalVar", value: "获取设备/全局变量" },
        { key: "DeleteEqGlobalVar", value: "删除单个设备/全局变量" },
        { key: "ImportEqVariable", value: "导入设备/平台变量数据" },        
        { key: "DownLoadEqVarData", value: "设备/平台变量导出" },
        { key: "PlcArrayDataExpand", value: "PLC变量展开事件" },
        { key: "EqArrayDataExpand", value: "设备变量展开事件" },
        { key: "BatchMonitor", value: "批量加入监控" },
        { key: "BatchDelete", value: "批量删除PLC变量" },
        { key: "BatchDownPlcData", value: "下载PLC变量" },
        { key: "BatchEqMonitor", value: "设备变量批量加入监控" },
        { key: "BatchEqArrayDelete", value: "批量删除设备/平台变量" },
        { key: "BatchDownEqData", value: "批量导出设备/平台变量" },
        { key: "CleanScriptDebugging", value: "清洗规则脚本调试" },
        { key: "GetCleanScript", value: "清洗规则脚本查询" },
        { key: "CleanScriptUpdate", value: "清洗规则脚本更新" },
        { key: "ChangePartConnect", value: "切换网络地址" },
        { key: "GetFiles", value: "获取文件" },
        { key: "GetLogsTable", value: "获取日志文件列表" },
        { key: "GetOptionValue", value: "获取搜索日志下拉框的内容" },
        { key: "89", value: "获取设备脚本目录树" },
        { key: "GetVarModelDesign", value: "获取设备下的所有变量" },
        { key: "GetMESExceptionLogList", value: "获取MES上传异常列表" },
        { key: "SendMesExceptionAgain", value: "重新发送MES异常的信息" },
        { key: "GetMonitorVar", value: "查询设备之下的PLC信息和所监控的变量" },
        { key: "GetOpLogs", value: "查询日志" },
        { key: "GetPlcMonitorVar", value: "获取监控中的PLC变量" },
        { key: "GetPlcMonitorVarByCode", value: "获取指定设备code的监控中的plc变量" },
        { key: "SendMesExceptionAgain", value: "重新发送MES异常的信息" },
        { key: "GetMonitorVar", value: "查询设备之下的PLC信息和所监控的变量" },
        { key: "GetOpLogs", value: "查询日志" },
        { key: "GetPlcMonitorVar", value: "获取监控中的PLC变量" },
        { key: "GetLineEqScriptInfo", value: "获取设备脚本目录树" },
        { key: "GetVarModelDesign", value: "获取设备下的所有变量树" },
        { key: "GetScriptVar", value: "获取设备下所有变量集合" },
        { key: "UpdateVarTriggers", value: "更新触发变量" },
        { key: "GetVarTriggers", value: "获取当前脚本的触发变量ID集合" },
        { key: "InsetScriptInfo", value: "新增脚本" },
        { key: "GetScriptInfo", value: "获取脚本相关设置内容" },
        { key: "PublishScriptContent", value: "发布脚本内容" },
        { key: "StartOrStopEq", value: "停止/启动设备" },
        { key: "GetEqState", value: "获取设备所有运行状态" },       
        { key: "CancelPublish", value: "取消脚本发布" },
        { key: "UpdateScriptInfo", value: "更新脚本" },
        { key: "DeleteScriptInfo", value: "删除脚本" },
        { key: "UpdateScritContent", value: "保存脚本内容" },
        { key: "GetAlarmQuerySearchInfo", value: "报警查询" },
        { key: "GetAlarmQueryList", value: "报警查询" },
        { key: "InsertEq", value: "新增设备" },
        { key: "InsertLine", value: "新增产线" },
        { key: "DeleteLineInfo", value: "删除产线信息" },
      ],
      startTime: format(sDaysAgo), // 起始时间，默认为7天前
      endTime: format(today), // 结束时间，默认为明天
      userName: "",
      menuName: "",
      pageNum: 1,
      functionName: "",
      pageSize: 20,
      total: 1,
      tableData: [],
      searchConditionLast: {
        userName: "",
        menuName: "",
        functionName: "",
        startTime: "",
        endTime: "",
        pageSize: 0,
        currentPage: 0,
      },
    };
  },
  watch: {},
  computed: {},
  methods: {
    table_index(index) {
      return (this.pageNum - 1) * this.pageSize + index + 1;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getOplog();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getOplog();
    },
    getOplog() {
        // 将字符串时间转换为 Date 对象
        const startDateTime = new Date(this.startTime);
        const endDateTime = new Date(this.endTime);

        // 获取当天的零点和最后时刻
        const startOfDay = new Date(startDateTime);
        startOfDay.setHours(0, 0, 0, 0);

        const endOfDay = new Date(endDateTime);
        endOfDay.setHours(23, 59, 59, 999);


      let param = {
        userName: this.userName,
        menuName: this.menuName ==''?[]:this.dicMenu
        .filter(item => item.value.includes(this.menuName))
        .map(item => item.key).length == 0 ? ['区分为空']:this.dicMenu
        .filter(item => item.value.includes(this.menuName))
        .map(item => item.key),
        functionName: this.functionName ==''?[]:this.dicFunc
        .filter(item => item.value.includes(this.functionName))
        .map(item => item.key).length == 0 ? ['区分为空']:this.dicFunc
        .filter(item => item.value.includes(this.functionName))
        .map(item => item.key),
        startTime: this.startTime == null ? null : this.$moment(new Date(startOfDay))
        .utcOffset("+08:00")
        .format("YYYY-MM-DD HH:mm:ss"),
        endTime: this.endTime == null ? null : this.$moment(new Date(endOfDay))
        .utcOffset("+08:00")
        .format("YYYY-MM-DD HH:mm:ss"),
        pageSize: this.pageSize,
        currentPage: this.pageNum,
      };

      let isDifferent = false;
      for (let key in param) {
        if (
          key !== "currentPage" &&
          JSON.stringify(param[key]) !==
            JSON.stringify(this.searchConditionLast[key])
        ) {
          isDifferent = true;
          break;
        }
      }

      if (isDifferent) {
        this.pageNum = 1;
        param.currentPage = 1;
      }

      this.searchConditionLast = param;

      $http
        .post("/OperationalLog/GetOpLogs", param)
        .then((res) => {
          if (res.Data) {
            this.tableData = res.Data.pageData;
            this.total = res.Data.totalCount;
          } else {
            this.$message.error("操作日志查询结果不存在");
            this.tableData = [];
            this.total = 1;
          }
        })
        .catch((err) => {
          this.$message.error("操作日志查询存在异常，异常原因为", err.message);
          this.tableData = [];
          this.total = 1;
        });
    },
  },
  created() {},
  mounted() {
    this.getOplog();
  },
};
</script>
